package com.mvc.service.healthmanager.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.huangqj.classification.Pressure;
import org.huangqj.consant.StringUtils;
import org.huangqj.consant.ValidatorUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.mvc.dao.BaseDaoI;
import com.mvc.dao.mapper.BloodPressureDao;
import com.mvc.framework.constant.GlobalConstant;
import com.mvc.model.healthmanager.archive.BloodPressure;
import com.mvc.service.healthmanager.BloodPressureServiceI;
import com.mvc.utils.CreatHighChartJson;
import com.mvc.utils.StringUtil;
/**
 * @author hejie
 *
 */
@Service
public class BloodPressureServiceImpl implements BloodPressureServiceI{
	private final String titleName = "血压数据走势";
	private final String unit = "mmHg/L";
	private final String yAxixname = "血压(mmHg/L)";
	@Autowired
	private BloodPressureDao bpDao;

	@Autowired
	private BaseDaoI<BloodPressure> pDaoI;
	@Override
	public List<BloodPressure> getBloodPressure(BloodPressure bPressure) {		
		return bpDao.getBloodPressure(bPressure);
	}
	@Override
	public List<BloodPressure> getMaxBloodPressure(BloodPressure bPressure) {
		
		return bpDao.getMaxBloodPressure(bPressure);
	}
	
	/**
	 * 获取最新的血压，根据个人id
	 * @author qingjie
	 * @param bPressure
	 * @return
	 * Date:2015-11-14
	 */
	@Override
	public BloodPressure getNewBloodPressure(BloodPressure bPressure) {
		Map<String, Object> params = StringUtils.beanToMap(bPressure);
		List<BloodPressure> list = pDaoI.find(StringUtil.getHql("BloodPressure", params, "desc"),params);
		return list.size()>0?list.get(0):new BloodPressure();
	}
	
	/**
	 * 根据个人id,获取数据并且拼接json字符串
	 * @author qingjie
	 * @param personId
	 * @return
	 * Datte:2015-11-15
	 */
	@Override
	public String getDataToJson(String personId) {
		List<BloodPressure> list = bpDao.getJsonData(personId);
		String[] xAxis = new String[list.size()];
		Map<String, Object> map = new HashMap<String, Object>();
		if(!list.isEmpty()){
			
			Map<String, Object> ls_m = new HashMap<String, Object>();
			ls_m.put("name", "左臂收缩压(mmHg)");
			List<Double> ls_l = new ArrayList<Double>();
			Map<String, Object> ld_m = new HashMap<String, Object>();
			ld_m.put("name", "左臂舒张压(mmHg)");
			List<Double> ld_l = new ArrayList<Double>();
			Map<String, Object> rs_m = new HashMap<String, Object>();
			rs_m.put("name", "右臂收缩压(mmHg)");
			List<Double> rs_l = new ArrayList<Double>();
			Map<String, Object> rd_m = new HashMap<String, Object>();
			rd_m.put("name", "右臂舒张压(mmHg)");
			List<Double> rd_l = new ArrayList<Double>();
			for (int i=0;i< list.size();i++) {
				 BloodPressure bp = list.get(i);
				 xAxis[i] = GlobalConstant.SDF.format(bp.getCreatDate());
				 ls_l.add(i, bp.getL_sbp());
				 ld_l.add(i, bp.getL_dbp());
				 rs_l.add(i, bp.getR_sbp());
				 rd_l.add(i, bp.getR_dbp());
				 
			}
			ls_m.put("data", ls_l);ld_m.put("data", ld_l);rs_m.put("data", rs_l);rd_m.put("data", rd_l);
			map.put("ls", ls_m);map.put("ld", ld_m);map.put("rs", rs_m);map.put("rd", rd_m);
		}
		return CreatHighChartJson.CreatJsonData(titleName, unit, yAxixname, xAxis, map);
	}
	
	@Override
	public List<BloodPressure> findNewBloodPressure(Map<String, Object> params,
			String order) {
		return pDaoI.find(StringUtil.getHql("BloodPressure", params,order),params);
	}	
	
	/**
	 * @author Roy
	 * 根据不同条件查询血压表
	 * @param param
	 * @return
	 */
	@Override
	public List<BloodPressure> findBloodPressure(String personId) {
		return pDaoI.find("from BloodPressure b where b.personId = '"+personId+"' order by b.count desc");
	}
	
	@Override
	public BloodPressure saveOrUpdate(BloodPressure bp) {
		List<BloodPressure> list =  pDaoI.find("from BloodPressure t where t.personId = '"+bp.getPersonId()+"' order by t.id desc");
		if(list.size()>0){
			BloodPressure b = list.get(0);
			int count = 1;
			if(!ValidatorUtil.isNullOnIntger(b.getCount())){
				count = b.getCount()+1;
			}
			bp.setCount(count);
		}
		bp.setDataSource("手机App");
		
		//血压状态
		double sbp = ValidatorUtil.isNullOnDouble(bp.getL_sbp())?bp.getR_sbp():bp.getL_sbp();
		double dbp = ValidatorUtil.isNullOnDouble(bp.getL_dbp())?bp.getR_dbp():bp.getL_dbp();
		bp.setPressureState(Pressure.evaluation(sbp, dbp)+"");
		bp.setModifyDate(bp.getCreatDate());
		try {
			bp.setNext_serviceDate(GlobalConstant.SDF2.parse(StringUtil.getNextTime(GlobalConstant.SDF2.format(bp.getCreatDate()), 3)));
		} catch (Exception e) {
			e.printStackTrace();
		}
		pDaoI.saveOrUpdate(bp);
		return bp;
	}	
}
